import {observable, action, runInAction} from 'mobx';
import {WeaTools} from 'ecCom';
import {WeaForm} from 'comsMobx';
import {Modal} from 'antd';
import * as urls from '../constants/ServerUrls';
import SearchFavourite from '../util/SearchFavourite';
import {WeaLocaleProvider} from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
const {callApi} = WeaTools;

export default class SysFavFormStore {
    @observable favid = '';
    @observable pagename = '';
    @observable favouritetype = '';
    @observable favtypename = '';
    @observable dirid = '';
    @observable favouriteObjid = '';
    @observable typelist = [];

    @observable spinning = false;
    @observable tip = '';

    @observable form = null;

    /**
     * SysFavouriteStore的实例
     */
    sysFavouriteStore = null;

    /**
     * FavouriteDialogStore的实例
     */
    favouriteDialogStore = null;

    constructor(sysFavouriteStore, favouriteDialogStore) {
        this.sysFavouriteStore = sysFavouriteStore;
        this.favouriteDialogStore = favouriteDialogStore;
        this.form = new WeaForm();
    }

    // 重置表单数据
    @action resetForm() {
        runInAction(() => {
            this.favid = '';
            this.pagename = '';
            this.favouritetype = '';
            this.favtypename = '';
            this.dirid = '';
            this.favouriteObjid = '';
            this.typelist = [];
        });
    }

    /**
     * 根据id查询收藏的信息
     * @param id
     */
    @action loadForm = (id) => {
        this.spinning = true;
        this.tip = `${getLabel(82275, '数据加载中，请稍候...')}`;
        const loadUrl = urls.GET_FAVOURITE;
        callApi(loadUrl, 'POST', {id}).then((resdata) => {
            runInAction(() => {
                this.spinning = false;
                this.tip = '';
                // 设置表单数据
                this.favid = resdata.favid;
                this.pagename = resdata.pagename;
                this.favouritetype = resdata.favouritetype;
                this.favtypename = resdata.favtypename;
                this.dirid = resdata.dirid;
                this.favouriteObjid = resdata.favouriteObjid;
                this.typelist = resdata.typelist;
            });
        });
    }

    /**
     * 保存表单数据
     * @param values
     */
    @action saveForm() {
        let values = this.form.getFormParams();
        const {pagename, favouritetype} = values;

        if (!pagename) {
            Modal.error({content: `${getLabel(385876, '收藏标题不能为空！')}`});
            return;
        }

        const editUrl = urls.EDIT_FAVOURITE;
        this.spinning = true;
        this.tip = `${getLabel(385878, '数据保存中，请稍候...')}`;
        const favid = this.favid;
        const favtypename = this.favtypename;
        const dirid = this.dirid;
        const favouriteObjid = this.favouriteObjid;
        values = {...values, favid, favtypename, dirid, favouriteObjid};
        callApi(editUrl, 'POST', values).then((resdata) => {
            this.spinning = false;
            this.tip = '';
            // 关闭窗口
            this.favouriteDialogStore.closeEditDialog();
            // 重置数据
            this.resetForm();
            /**
             * 刷新列表
             */
            const isinit = false;
            const isreload = true;
            const searchFavourite = new SearchFavourite(this.sysFavouriteStore);
            const paras = {isinit, isreload};
            searchFavourite.load(paras);
        });
    }

    /**
     * 表单字段更新时，保存表单字段的值
     * @param values
     */
    @action saveFormFields = (fields = {}) => {
        // 将变更的字段的值，存入state中
        for (const key in fields) {
            switch (key) {
                case 'pagename':
                    this.pagename = fields[key].value;
                    break;
                case 'favtype':
                    this.favouritetype = fields[key].value;
                    break;
                default:
                    this[key] = fields[key].value;
            }
        }
    }
}
